@extends('layout.default')

@section('layout.content') 

<div class="row">
  <div class="col-xs-12">
    <input type="hidden" name="_method" value="put" />
    <table class="table table-hover table-bordered table-striped">
      <thead>
        <tr>
          <th style="min-width:130px;">权限名</th>
          <th style="min-width:120px;">控制名</th>
          <th style="min-width:90px;">模块</th>
          <th style="min-width:90px;">显示名称</th>
          <th class="center" style="min-width:50px;">排序</th>
          <th class="center" style="min-width:50px;">状态</th>
        </tr>
      </thead>

      <tbody>
      @foreach($permissions as $permission)
        <?php
          $childCount = count($permission->children);
          if($childCount <= 0) $childCount = 1;
          $rowspan = $childCount == 1 ? '' : "rowspan='{$childCount}'";
          $child = $permission->children->shift();
        ?>
        <tr>          
          <td {!! $rowspan !!}><b>{{ $permission->title }}</b>（{{ $permission->controller }}）</td>
          <td class="td-permission">
            {{ $child->title }}
            <div class="pull-right operate">
              <a href="javascript:;" class="red edit-permission" data-id="{{$child->id}}"><i class="fa iconfont">&#xe760;</i></a>
            </div>
          </td>
          <td>{{ $child->action }}</td>
          <td>{{ $child->subtitle }}</td>
          <td class="center">{{ $child->order}}</td>
          <td class="center">{!! $child->present()->permissionStatus !!}</td>          
        </tr>
        @if($rowspan != '')
          @foreach($permission->children as $child)
          <tr>
            <td class="td-permission">
              {{ $child->title }}
              <div class="pull-right operate">
                <a href="javascript:;" class="red edit-permission" data-id="{{$child->id}}"><i class="fa iconfont">&#xe760;</i></a>
              </div>
            </td>
            <td>{{ $child->action }}</td>
            <td>{{ $child->subtitle }}</td>
            <td class="center">{{ $child->order}}</td>
            <td class="center">{!! $child->present()->permissionStatus !!}</td>   
          </tr>
          @endforeach
        @endif
      @endforeach
      </tbody>
    </table>
    
  </div><!-- /.col -->
</div><!-- /.row -->
@stop

@section('javascript')
<script type="text/javascript">

  $(function() {

    $('.edit-permission').on('click', function() {
      var id = $(this).data('id');
      var uri = '/system/permission/'+id+'/edit';      
      layer.open({
          type: 2,
          title: '权限设置',
          area: ['360px', '560px'],
          shade: 0.8,
          content: uri,
        });      
    });


    $('.status').on('click', function() {
      var that = $(this);
      var id = that.data('id');
      var uri = '/system/permission/change/'+id;
      $.post(uri, {
        '_token':$("input[name='_token']").attr('value'),
        '_method': $("input[name='_method']").attr('value'),
        'is_submit': true
      }, function(data) {
          if (data.status === true) {
            if(data.data.status == 1)
              that.parent().attr('class','red');
            else
              that.parent().attr('class','green');
          }
      },'json').error(function(data){
        layer.msg(data.responseJSON.message);
      }); 
    });
    
    
    $('.td-permission').on('mouseenter', function () {
        $(this).find('.operate').show();
    }).on('mouseleave', function () {
        $(this).find('.operate').hide();
    });
    
  });



</script>
@stop